/**
 * 
 */
package durak.server.control;

import java.util.ArrayList;
import java.util.List;

import durak.server.model.GameDesc;
import durak.server.model.GamingTable;
import durak.server.model.Player;

/**
 * Game class is responsible for the process of adding new human players to the
 * game holds a reference to commandHandler holds a reference to gamingTable -
 * initiates gamingTable
 */

public class Game {
	
	public enum playerType {
		HUMAN, COMPUTER
	}
	
	private String name;
	private int numOfHumanPlayers;
	private int numOfAutoPlayers;
	private int numOfConnectedPlayers;
	private List<PlayerControl> players;
	private List<String> expectedHumanPlayersName; //for XML load use
	private Boolean isFromXML;
	private CommandHandler commandHandler;
	private GamingTable gamingTable;

	public GameDesc getGameDescription() {
		GameDesc gameDesc = new GameDesc();
		gameDesc.setName(getName());
		gameDesc.setNumOfAutoPlayers(numOfAutoPlayers);
		gameDesc.setNumOfHumanPlayers(numOfHumanPlayers);
		gameDesc.setNumOfConnectedPlayers(numOfConnectedPlayers);
		gameDesc.setIsFromXML(isFromXML);
		gameDesc.setHumanPlayersNames(expectedHumanPlayersName);
		return gameDesc;
	}
	
	public GamingTable getGamingTable() {
		return gamingTable;
	}

	/**
	 * 
	 */
	public Game() {
		gamingTable = new GamingTable();
		commandHandler = new CommandHandler(this);
	}

	/**
	 * @param numOfHumanPlayers
	 * @param numOfAutoPlayers
	 * @param server2ClientsCommunication
	 */
	public Game(int numOfPlayers, int numOfAutoPlayers,
			Server2ClientsCommunication server2ClientsCommunication) {
		super();
		this.numOfHumanPlayers = numOfPlayers;
		this.numOfAutoPlayers = numOfAutoPlayers;
		this.players = new ArrayList<PlayerControl>();
		this.gamingTable = new GamingTable();
		this.commandHandler = new CommandHandler(this);
	}

	public Game(String gameName, Integer numOfPlayers, Integer numOfAutoPlayers, Boolean isFromXML) {
		super();
		this.name = gameName;
		this.numOfHumanPlayers = numOfPlayers;
		this.numOfAutoPlayers = numOfAutoPlayers;
		this.isFromXML=isFromXML;
		this.players = new ArrayList<PlayerControl>();
		this.expectedHumanPlayersName = new ArrayList<String>();
		this.gamingTable = new GamingTable();
		this.commandHandler = new CommandHandler(this);
	}

	public boolean isGameFull() {
		return (numOfConnectedPlayers == (numOfHumanPlayers + numOfAutoPlayers));
	}

	public boolean addPlayer(PlayerControl player) {
		if (!isGameFull()) {
			players.add(player);
			gamingTable.addPlayer(player.getPlayer());
			numOfConnectedPlayers++;
			return true;
		}
		return false;
	}

	public CommandHandler getCommandHandler() {
		return commandHandler;
	}

	public PlayerControl getPlayerControl(Player currentDefender) {
		for (PlayerControl playerControl : players) {
			if (playerControl.getPlayer() == currentDefender)
				return playerControl;
		}
		return null;
	}

	public List<PlayerControl> getPlayers() {
		return players;
	}

	public boolean allPlayersReady() {
		for (PlayerControl player : players) {
			if (player.isInitialized() == false)
				return false;
		}
		return true;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PlayerControlHuman getPlayerByName(String name) {
		for (PlayerControl playerControl : players) {
			if (playerControl.getPlayer().getName().compareToIgnoreCase(name) == 0)
				return (PlayerControlHuman) playerControl;
		}
		return null;
	}
	
	public Boolean getIsFromXML() {
		return isFromXML;
	}

	public void setIsFromXML(Boolean isFromXML) {
		this.isFromXML = isFromXML;
	}
	
	public List<String> getExpectedHumanPlayersName() {
		return expectedHumanPlayersName;
	}
	
	public void addExpectedHumanPlayersName(String name) {
		expectedHumanPlayersName.add(name);
	}
	
	


}
